package com.ccp.dev.core.basic.response;

import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.List;

/**
 * @Author: dfz
 * @Date: 2019/4/24 14:50
 * @Description:统一返回类
 */
public class ResultData implements Serializable {
    private static final long serialVersionUID = 1L;
    /**
     * 状态码
     */
    protected int code;
    /**
     * 返回提示内容
     */
    protected String msg;
    /**
     * 总数据条数
     */
    protected long count;
    /**
     * 返回的数据
     */
    protected Object data;

    /**
     * 功能描述:  无参构造
     *
     * @return: com.ccp.dev.portal.response.ResultData
     * @auther: dfz
     * @date: 2019/4/24 15:02
     */
    public ResultData() {

    }

    /**
     * 功能描述:  有参构造
     * @author nd
     * @param code 状态码
     * @param msg 返回提示内容
     * @param count 总数据条数
     * @param data 返回的数据
     */
    public ResultData(int code, String msg, long count, Object data) {
        this.code = code;
        this.msg = msg;
        this.count = count;
        this.data = data;
    }

    /**
     * 功能描述: 操作成功后，指定返回提示内容
     *
     * @param: msg
     * @return: com.ccp.dev.portal.response.ResultData
     * @auther: dfz
     * @date: 2019/4/24 15:03
     */
    public static ResultData success(String msg) {

        ResultData resultData = new ResultData();
        resultData.setCode(ReturnCode.SUCCESS.getCode());
        if (StringUtils.isNotEmpty(msg)) {
            resultData.setMsg(msg);
        } else {
            resultData.setMsg(ReturnCode.SUCCESS.getMsg());
        }
        return resultData;
    }

    /**
     * 功能描述: 操作成功后，指定返回提示内容和结果集
     *
     * @param: [msg, dataList]
     * @return: com.ccp.dev.portal.response.ResultData
     * @auther: dfz
     * @date: 2019/4/24 15:05
     */
    public static ResultData success(String msg, Object dataObject) {

        ResultData resultData = new ResultData();
        resultData.setCode(ReturnCode.SUCCESS.getCode());
        if (StringUtils.isNotEmpty(msg)) {
            resultData.setMsg(msg);
        } else {
            resultData.setMsg(ReturnCode.SUCCESS.getMsg());
        }
        resultData.setData(dataObject);
        if (dataObject instanceof List) {
            resultData.setCount(((List) dataObject).size());
        }
        return resultData;
    }

    /**
     * 功能描述: 操作成功后，指定返回结果集
     *
     * @param: [list]
     * @return: com.ccp.dev.portal.response.ResultData
     * @auther: dfz
     * @date: 2019/4/24 15:06
     */
    public static ResultData success(Object list) {

        ResultData resultData = new ResultData();
        resultData.setCode(ReturnCode.SUCCESS.getCode());
        resultData.setMsg(ReturnCode.SUCCESS.getMsg());
        resultData.setData(list);
        return resultData;
    }

    /**
     * 功能描述: 操作成功后，指定返回结果集
     *
     * @param: [list]
     * @return: com.ccp.dev.portal.response.ResultData
     * @auther: dfz
     * @date: 2019/4/24 15:06
     */
    public static ResultData success(Object list, int total) {

        ResultData resultData = new ResultData();
        resultData.setCode(ReturnCode.SUCCESS.getCode());
        resultData.setMsg(ReturnCode.SUCCESS.getMsg());
        resultData.setData(list);
        resultData.setCount(total);
        return resultData;
    }

    /**
     * 功能描述: 操作成功后，返回默认提示内容
     *
     * @return: com.ccp.dev.portal.response.ResultData
     * @auther: dfz
     * @date: 2019/4/24 15:07
     */
    public static ResultData success() {

        return ResultData.success(ReturnCode.SUCCESS.getMsg());
    }

    /**
     * 功能描述: 操作失败后，返回指定状态码和提示内容
     *
     * @param: [code, msg]
     * @return: com.ccp.dev.portal.response.ResultData
     * @auther: dfz
     * @date: 2019/4/24 15:07
     */
    public static ResultData error(int code, String msg) {
        ResultData resultData = new ResultData();
        resultData.setCode(code);
        resultData.setMsg(msg);
        return resultData;
    }

    /**
     * 功能描述: 操作失败，返回失败的默认状态码和提示内容
     *
     * @return: com.ccp.dev.portal.response.ResultData
     * @auther: dfz
     * @date: 2019/4/24 15:08
     */
    public static ResultData error() {

        return error(ReturnCode.FAIL.getCode(), ReturnCode.FAIL.getMsg());
    }

    /**
     * 功能描述: 操作失败，返回指定提示内容
     *
     * @param: [msg]
     * @return: com.ccp.dev.portal.response.ResultData
     * @auther: dfz
     * @date: 2019/4/24 15:09
     */
    public static ResultData error(String msg) {

        return error(ReturnCode.FAIL.getCode(), msg);
    }

    /**
     * 功能描述: 带返回数据的构造方法
     *
     * @param list  列表数据
     * @param total 总记录数
     * @return:
     * @auther: dfz
     * @date: 2019/4/24 15:09
     */
    public ResultData(List<?> list, int total) {
        this.data = list;
        this.count = total;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public long getCount() {
        return count;
    }

    public void setCount(long count) {
        this.count = count;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }
}
